package Java8实战._1_Lambda._7_使用Lambda;

import Java8实战._1_Lambda.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _7_使用Lambda {

    public static void main(String[] args) {
        List<Apple> inventory = Arrays.asList(
                new Apple("red",150),
                new Apple("green",123),
                new Apple("green",189)
        );

        List<Apple> result;

        result = filterApples(inventory, (Apple apple) -> "green".equals(apple.getColor()));

        printApples(result, (Apple apple) -> "苹果的颜色：" + apple.getColor() + "，苹果的重量：" + apple.getWeight());
    }

    public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate applePredicate){

        List<Apple> list = new ArrayList<>();
        for (Apple apple : inventory){
            if(applePredicate.test(apple)){
                list.add(apple);
            }
        }
        return list;
    }

    public static void printApples(List<Apple> list, AppleFormatter appleFormatter){

        for (Apple apple : list){
            System.out.println(appleFormatter.accept(apple));
        }
    }

}
